<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>属性列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="templateId" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="属性模板" prop="templateId">
        <el-select v-model="ruleForm.templateId" placeholder="请选择属性模板以查询对应的属性数据">
          <el-option
              v-for="item in attributeTemplateListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="loadAttributeListByTemplateId()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID" width="50px" align="center"></el-table-column>
      <el-table-column prop="templateId" label="属性模板id" width="140px" align="center"></el-table-column>
      <el-table-column prop="name" label="属性名称" width="140px" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" align="center"></el-table-column>
      <el-table-column prop="type" label="属性类型" align="center"></el-table-column>
      <el-table-column prop="inputType" label="输入类型" align="center"></el-table-column>
      <el-table-column prop="valueList" label="备选值列表" align="center"></el-table-column>
      <el-table-column prop="unit" label="计量单位" align="center"></el-table-column>
      <el-table-column prop="sort" label="自定义排序序号" align="center"></el-table-column>
      <el-table-column prop="isAllowCustomize" label="是否允许自定义" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="120px">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="small"></el-button>
          <el-button type="danger" icon="el-icon-delete"
                     circle size="small" @click="attributeDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      attributeTemplateListOptions: [],
      ruleForm: {
        templateId: '',
        name: '',
        description: '',
        type: '',
        inputType: '',
        valueList: '',
        unit: '',
        sort: '',
        isAllowCustomize: ''
      }
    };
  },
  methods: {
    attributeDeleteConfirm(attribute){
      let message = '此操作将永久删除'+attribute.name+'文件, 是否继续?'
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleAttributeDelete(attribute);
      })
    },
    handleAttributeDelete(attribute){
      console.log("准备删除id为"+attribute.id+"属性")
      this.axios.post('http://localhost:9080/attributes/'+attribute.id+'/deleteById').then(
          (response)=>{
            let responseBody = response.data;
            if (responseBody.state != 20000){
              this.$message.error(responseBody.message)
            }else{
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
            if(responseBody.state==20000 || responseBody.state == 40400){
              this.loadAttributeListByTemplateId();
            }
          }
      )
    },
    loadAttributeTemplateList() {
      let url = "http://localhost:9080/AttributeTemplate"
      console.log(url);
      this.axios
          .create({
            'headers':{
              'Authorization': localStorage.getItem('jwt')
            }
          })
          .get(url).then((response) => {
            let responseBody = response.data;
            this.attributeTemplateListOptions = responseBody.data;
          }
      )
    },
    loadAttributeListByTemplateId() {
      let url = "http://localhost:9080/attributes/" + this.ruleForm.templateId
      console.log(url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        let attributeString = JSON.stringify(this.ruleForm)
        console.log('templateId: '+this.ruleForm.templateId)
        console.log('attributeString' + attributeString)
        this.tableData = responseBody.data;
      })
    }
  },
  mounted() {
    this.loadAttributeTemplateList();
  }
}
</script>
